package com.springcloud.model.util;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

import java.io.IOException;

public class JsonUtil {

    //多线程安全
    private static ObjectMapper objectMapper = new ObjectMapper();

    private JsonUtil(){}

    public static String obj2Json(Object obj) throws IOException {
        if (obj != null) {
            return objectMapper.writeValueAsString(obj);
        }
        return null;
    }


    public static <T> T json2Obj(String json, Class<T> clazz) throws IOException {
        return objectMapper.readValue(json, clazz);
    }

    public static <T> T convertValue(Object fromValue, TypeReference toValueTypeRef){
        return objectMapper.convertValue(fromValue,toValueTypeRef);
    }

    public static void main(String[] args){

    }

}
